import { Controller, Get, Post, Put, Del, Inject, Body, Param, Query } from '@midwayjs/decorator';
import { GatewayService } from '../../service/firmware/gateway.service';
import { resBuild } from '@utils/resBuild';
import { getGatewayByMac_Wb } from '@dto/firmware/gateway.dto';
import { ListWristbandDevDTO } from '@dto/firmware/wristband_dev.dto';

@Controller('/gateway_wristband')
export class GatewayWristbandController {
  @Inject()
  gatewayService: GatewayService;

  // **创建网关**
  @Post('/create')
  async create(@Body() body) {
    try {
      await this.gatewayService.createGateway(body);
      return resBuild.success("创建成功");
    } catch (error) {
      return resBuild.fail(error)
    }
  }

  // **获取所有网关**
  @Get('/list')
  async list(@Query() params) {
    console.log(11111,params)
    const gateways = await this.gatewayService.getAllGateways(params);
    return resBuild.data(gateways);
  }


  // **获取所有网关及关联手环**  分页
  @Get('/list_wb')
  async list_wb(@Query() queryParams: getGatewayByMac_Wb) {
    const gateways = await this.gatewayService.getAllGateways_Wb(queryParams);
    return resBuild.data(gateways);
  }


  // **获取单个网关及关联手环**
  @Get('/gw_wb')
  async getByMac_Wb(@Query() queryParams: ListWristbandDevDTO) {
    const gateway = await this.gatewayService.getGatewayByMac_Wb(queryParams);
    if (!gateway) return resBuild.fail("网关不存在!");
    return resBuild.data(gateway);
  }

  // **获取单个网关**
  @Get('/:mac')
  async getByMac(@Param('mac') mac: string) {
    const gateway = await this.gatewayService.getGatewayByMac(mac);
    if (!gateway) return resBuild.fail("网关不存在!");
    return resBuild.data(gateway);
  }

  // **更新网关**
  @Put('/update/:mac')
  async update(@Param('mac') mac: string, @Body() body) {
    console.log('mac', mac)
    console.log('Body', body)
    try {
      const updatedGateway = await this.gatewayService.updateGateway(mac, body);
      if (!updatedGateway) return resBuild.fail("更新失败，网关不存在！");
      return resBuild.success("更新成功！");
    } catch (error) {
      return resBuild.fail("更新失败", error);
    }
  }

  // **删除网关**
  @Del('/delete/:mac')
  async delete(@Param('mac') mac: string) {
    const deleted = await this.gatewayService.deleteGateway(mac);
    if (!deleted) return resBuild.fail("删除失败，网关不存在!");
    return resBuild.success("删除成功!");
  }
}
